package com.transportation.app.net

import com.transportation.app.bean.BaseModel
import io.reactivex.rxjava3.observers.DisposableObserver

abstract class RetrofitRxSubscribe<T> : DisposableObserver<BaseModel<T>>() {
    
    override fun onNext(baseModel: BaseModel<T>) {
        if ("200" == baseModel.code) {
            onSuccess(baseModel.data)
            onFullSuccess(baseModel)
        } else {
            onFailed(baseModel.api_code, baseModel.api_msg)
        }
    }

    final override fun onComplete() {
    }

    final override fun onError(t: Throwable) {
        onFailed(-1, t.message)
    }

    /**
     * data回调方法
     */
    protected abstract fun onSuccess(t: T?)

    /**
     * BaseModel回调方法
     */
    protected open fun onFullSuccess(t: BaseModel<T>?) {

    }

    /**
     * 失败回调方法
     */
    protected open fun onFailed(code: Int, msg: String?) {

    }

}